﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.PM.PO;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForOrderToItem : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //采购单
            UFIDA.U9.PM.PO.PurchaseOrder order = key.GetEntity() as UFIDA.U9.PM.PO.PurchaseOrder;
            if (order == null) return;

            #region 采购审核后,把采购行的最终价(原币)写入该料品的私有4

            if (order.OriginalData.Status == PODOCStatusEnum.Approving && order.Status == PODOCStatusEnum.Approved)
            {
                foreach (var line in order.POLines)
                {
                    using (ISession session = Session.Open())
                    {
                        ItemMaster item = line.ItemInfo.ItemID;
                        if (item.DescFlexField.PrivateDescSeg5 == null || item.DescFlexField.PrivateDescSeg5 == "")
                        {
                            //最新采购价
                            item.DescFlexField.PrivateDescSeg4 = line.FinallyPriceTC.ToString();
                            //最新采购时间
                            item.DescFlexField.PrivateDescSeg5 = order.BusinessDate.Date.ToString("yyyy-MM-dd");
                            //最新供应商
                            item.DescFlexField.PrivateDescSeg10 = order.Supplier.Name.ToString();
                            //税率
                            item.DescFlexField.PrivateDescSeg16 = line.TaxRate.ToString();
                            //未税单价
                            item.DescFlexField.PrivateDescSeg17 = line.PriceDifferenceTC.ToString();
                        }
                        else if (DateTime.Parse(item.DescFlexField.PrivateDescSeg5) < order.BusinessDate)
                        {
                            //最新采购价
                            item.DescFlexField.PrivateDescSeg4 = line.FinallyPriceTC.ToString();
                            //最新采购时间
                            item.DescFlexField.PrivateDescSeg5 = order.BusinessDate.Date.ToString("yyyy-MM-dd");
                            //最新供应商
                            item.DescFlexField.PrivateDescSeg10 = order.Supplier.Name.ToString();
                            //税率
                            item.DescFlexField.PrivateDescSeg16 = line.TaxRate.ToString();
                            //未税单价
                            item.DescFlexField.PrivateDescSeg17 = line.PriceDifferenceTC.ToString();
                        }

                        session.Commit();
                    }

                }
            }
           



            #endregion

        }

    }
}
